Collections in Golo
Getting Started with Golo
GoloScript offers several collection types for different use cases:
- Tuples: Ordered immutable collections
- Arrays: Ordered mutable collections with fast indexing
- Lists: Ordered mutable collections with functional operations (map, filter)
- Vectors: Ordered mutable collections optimized for numeric data
- Sets: Unordered collections with no duplicates
- Maps: Key-value pair collections
Tuples (immutable)
Tuples cannot be modified after creation. Operations return new tuples.
let fruits = ["apple", "banana", "orange"]
println(fruits: get(0)) # apple
println(fruits: size()) # 3
println(fruits: contains("banana")) # true
println(fruits: tail()) # [banana, orange]
# extend returns a NEW tuple
let more = fruits: extend("grape")
println(fruits) # [apple, banana, orange] (unchanged)
println(more) # [apple, banana, orange, grape]
Arrays (mutable)
Arrays support fast index access and in-place modification.
let numbers = array[1, 2, 3, 4, 5]
println(numbers: get(2)) # 3
# set modifies the array in place
numbers: set(2, 99)
println(numbers) # array[1, 2, 99, 4, 5]
# append returns a NEW array
let more = numbers: append(6)
Lists (mutable, with map/filter)
Lists support functional operations like map and filter.
let numbers = list[1, 2, 3, 4, 5]
let doubled = numbers: map(|x| -> x * 2)
println(doubled) # list[2, 4, 6, 8, 10]
let evens = numbers: filter(|x| -> (x % 2) == 0)
println(evens) # list[2, 4]
# Chaining
let result = numbers
: filter(|x| -> x > 2)
: map(|x| -> x * 10)
println(result) # list[30, 40, 50]
Vectors
Vectors work like arrays but are optimized for numeric data.
let vec = vector[1.5, 2.5, 3.5]
println(vec: get(0)) # 1.5
vec: set(0, 9.9)
println(vec) # vector[9.9, 2.5, 3.5]
Sets
Sets automatically eliminate duplicates. Order is not guaranteed.
let numbers = set[1, 2, 3, 2, 1]
println(numbers) # set[1, 2, 3]
Maps
Maps store key-value pairs.
let person = map[["name", "Alice"], ["age", 30]]
println(person: get("name")) # Alice
println(person: containsKey("email")) # false
foreach key in person: keys() {
println(key + ":", person: get(key))
}
Collection Comprehensions
Comprehensions create collections with a concise syntax.
# Double each number
let doubles = list[x * 2 foreach x in [1, 2, 3, 4, 5]]
# list[2, 4, 6, 8, 10]
# Filter with "when"
let evens = list[x foreach x in [1..10] when x % 2 == 0]
# list[2, 4, 6, 8, 10]
# Nested foreach (cartesian product)
let pairs = list[[x, y] foreach x in [1..3] foreach y in [1..3] when x < y]
# [[1,2], [1,3], [2,3]]